package com.amos.isearch.main;

import com.hankcs.hanlp.HanLP;
import com.hankcs.hanlp.dictionary.py.Pinyin;
import com.hankcs.hanlp.summary.TextRankSentence;
import com.hankcs.hanlp.tokenizer.IndexTokenizer;
import com.hankcs.hanlp.tokenizer.SpeedTokenizer;

import java.util.List;

/**
 * DESCRIPTION: 汉语处理
 *
 * @author <a href="mailto:daoyuan0626@gmail.com">amos.wang</a>
 * @date 2021/1/23
 */
public class HanLPMain {

    public static void main(String[] args) {
        String content = "习近平在北京2022年冬奥会和冬残奥会筹办工作汇报会上作出六方面部署：第一，做好场馆建设和管理；第二，做好赛时运行工作；第三，做好各项赛会服务保障工作；第四，严格落实疫情防控各项措施；第五，提升冰雪运动发展水平；第六，推动京津冀协同发展。";
        System.out.println("原文内容：" + content);
        System.out.println("===================================================================");

        System.out.printf("标准分词：%s\n", HanLP.segment(content));
        System.out.printf("索引分词：%s\n", IndexTokenizer.segment(content));
        System.out.printf("极速词典分词：%s\n", SpeedTokenizer.segment(content));
        // System.out.println("NLP分词：");
        // System.out.println(NLPTokenizer.segment(content));
        // System.out.println(NLPTokenizer.analyze("我的希望是希望张晚霞的背影被晚霞映红").translateLabels());
        // System.out.println(NLPTokenizer.analyze("支援臺灣正體香港繁體：微软公司於1975年由比爾·蓋茲和保羅·艾倫創立。"));
        // System.out.printf("N-最短路径分词：%s\n", HanLP.extractKeyword(content, 5));
        // CRFLexicalAnalyzer analyzer = new CRFLexicalAnalyzer();
        // System.out.printf("CRF分词：%s\n", analyzer.analyze(content));
        System.out.println("===================================================================");

        System.out.printf("关键词提取：%s\n", HanLP.extractKeyword(content, 5));
        System.out.printf("自动摘要：%s\n", TextRankSentence.getTopSentenceList(content, 5));
        System.out.printf("短语提取：%s\n", HanLP.extractPhrase(content, 100));
        System.out.println("===================================================================");

        // 简繁切换
        System.out.printf("简体转繁体：%s\n", HanLP.convertToTraditionalChinese("用笔记本电脑写程序"));
        System.out.printf("繁体转简体：%s\n", HanLP.convertToSimplifiedChinese("「以後等妳當上皇后，就能買士多啤梨慶祝了」"));
        System.out.println("===================================================================");

        // 测试拼音相关
        pinyinTest(content);
    }

    private static void pinyinTest(String text) {
        List<Pinyin> pinyinList = HanLP.convertToPinyinList(text);
        System.out.print("原文：");
        for (char c : text.toCharArray()) {
            System.out.printf("%c,", c);
        }
        System.out.println();

        System.out.print("拼音（无音调）：");
        for (Pinyin pinyin : pinyinList) {
            System.out.printf("%s,", pinyin.getPinyinWithoutTone());
        }
        System.out.println();

        System.out.print("拼音（数字音调）：");
        for (Pinyin pinyin : pinyinList) {
            System.out.printf("%s,", pinyin);
        }
        System.out.println();

        System.out.print("拼音（符号音调）：");
        for (Pinyin pinyin : pinyinList) {
            System.out.printf("%s,", pinyin.getPinyinWithToneMark());
        }
        System.out.println();

        System.out.print("声调：");
        for (Pinyin pinyin : pinyinList) {
            System.out.printf("%s,", pinyin.getTone());
        }
        System.out.println();

        System.out.print("声母：");
        for (Pinyin pinyin : pinyinList) {
            System.out.printf("%s,", pinyin.getShengmu());
        }
        System.out.println();

        System.out.print("韵母：");
        for (Pinyin pinyin : pinyinList) {
            System.out.printf("%s,", pinyin.getYunmu());
        }
        System.out.println();

        System.out.print("输入法头：");
        for (Pinyin pinyin : pinyinList) {
            System.out.printf("%s,", pinyin.getHead());
        }
    }

}
